package com.dlu.safe_construction.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dlu.safe_construction.common.ApiResult;
import com.dlu.safe_construction.common.CommonEnum;
import com.dlu.safe_construction.common.utils.Utils;
import com.dlu.safe_construction.dto.IllRecordDTO;
import com.dlu.safe_construction.mapper.OwnerMapper;
import com.dlu.safe_construction.model.IllRecord;
import com.dlu.safe_construction.model.Owner;
import com.dlu.safe_construction.service.IllRecordService;
import com.dlu.safe_construction.service.OwnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by  lijiaxing on 2022/4/3 18:09
 *
 * @author lijiaxing
 */
@CrossOrigin
@RestController
@RequestMapping("/ill_record")
@Slf4j
public class IllRecordController implements BaseController {


    @Resource
    private IllRecordService illRecordService;

    @Resource
    private OwnerMapper ownerMapper;

    @GetMapping(path = "/add.json", produces = APPLICATION_JSON)
    public ApiResult<Void> add(@RequestBody IllRecord illRecord) {
        boolean ret = illRecordService.save(illRecord);
        if (ret)
            return ApiResult.success();
        else
            return ApiResult.fail(8000);
    }

    private List<IllRecordDTO> createIllRecordDTO(List<IllRecord> illRecords) {
        List<IllRecordDTO> illRecordDTOS = new ArrayList<>();
        illRecords.stream().forEach(illRecord -> {
            IllRecordDTO illRecordDTO = IllRecordDTO.fromIllRecord(illRecord);
            Owner owner = ownerMapper.selectByPrimaryKey(illRecord.getOwnerId());
            illRecordDTO.setOwnerName(owner.getOwnerName());
            illRecordDTO.setOwnerPhone(owner.getOwnerPhone());
            illRecordDTO.setScore(owner.getScore());
            illRecordDTO.setCarId(illRecord.getCarId());
            illRecordDTOS.add(illRecordDTO);
        });
        return illRecordDTOS;
    }

    @GetMapping(path = "/list.json", produces = APPLICATION_JSON)
    public ApiResult<List<IllRecordDTO>> list() {
        List<IllRecord> illRecords = illRecordService.list();
        return ApiResult.success(createIllRecordDTO(illRecords));
    }

    @GetMapping(path = "/listBy.json", produces = APPLICATION_JSON)
    public ApiResult<List<IllRecordDTO>> listBy(String key, Integer byWhat) {
        QueryWrapper<IllRecord> queryWrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        if (CommonEnum.IllRecordListBy.LIST_BY_ILL_RECORD_ID.isThisStatus(byWhat)) {
            map.put("record_id", Integer.valueOf(key));
            ApiResult.success(createIllRecordDTO(illRecordService.listByMap(map)));
        } else if (CommonEnum.IllRecordListBy.LIST_BY_OWNER_ID.isThisStatus(byWhat)) {
            queryWrapper.like("owner_id", key);
        } else if (CommonEnum.IllRecordListBy.LIST_BY_DATA.isThisStatus(byWhat)) {
            queryWrapper.like("date", key);
        } else if (CommonEnum.IllRecordListBy.LIST_BY_CAR_ID.isThisStatus(byWhat)) {
            queryWrapper.like("car_id", key);
        } else if (CommonEnum.IllRecordListBy.LIST_BY_OWNER_PHONE.isThisStatus(byWhat)) {
            List<Owner> owners = ownerMapper.selectByOwnerPhone(key);
            if (Utils.isNotEmpty(owners)) {
                owners.stream().forEach(owner -> {
                    queryWrapper.eq("owner_id", owner.getOwnerId()).or();
                });
                queryWrapper.eq("owner_id", owners.get(0).getOwnerId());
            } else {
                return ApiResult.success();
            }
        }
        return ApiResult.success(createIllRecordDTO(illRecordService.list(queryWrapper)));
    }

    @GetMapping(path = "/deleteBy.json", produces = APPLICATION_JSON)
    public ApiResult<Void> deleteBy(String key, Integer byWhat) {
        Map<String, Object> params = new HashMap<>();
        if (CommonEnum.IllRecordDeleteBy.DELETE_BY_ILL_RECORD_ID.isThisStatus(byWhat)) {
            params.put("record_id", Integer.valueOf(key));
        } else if (CommonEnum.IllRecordDeleteBy.DELETE_BY_OWNER_ID.isThisStatus(byWhat)) {
            params.put("owner_id", key);
        }
        boolean ret = illRecordService.removeByMap(params);

        if (ret)
            return ApiResult.success();
        else
            return ApiResult.fail(8000);
    }

    @PostMapping(value = "/edit.json", produces = APPLICATION_JSON)
    public ApiResult<Void> edit(@RequestBody IllRecord illRecord) {
        boolean ret = illRecordService.updateById(illRecord);
        if (ret)
            return ApiResult.success();
        else
            return ApiResult.fail(8000);
    }
}
